#include "stdafx.h"
#include "Log.h"
#include <assert.h>

CLog* CLog::m_pInstance = NULL;

#define BEGIN_LOG   try {
#define END_LOG			}	\
					catch(...) {	\
						cout << "Exception..." << endl;	\
						LOG4CPLUS_FATAL(m_logger, "LOG Exception occured...");	\
						}

CLog* CLog::Instance()
{
	if ( m_pInstance == NULL)
	{
		m_pInstance = new CLog();
	}
	return m_pInstance;
}

void  CLog::Release()
{
	if ( m_pInstance != NULL )
	{
		delete m_pInstance;
		m_pInstance = NULL;
	}
}

CLog::CLog()
{
	assert( m_pInstance==NULL);
}

bool CLog::InitLog()
{
	PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("../config/log4cplus.properties"));
	m_logger = Logger::getRoot();
	m_packetLogger = Logger::getInstance(LOG4CPLUS_TEXT("packetLogger"));
	m_efficiency   = Logger::getInstance(LOG4CPLUS_TEXT("efficiencyLogger"));
	m_closeSocket = Logger::getInstance( LOG4CPLUS_TEXT("closeSocket"));
	return true;
}

void CLog::LOG_WARN( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_WARN(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_INFO( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_INFO(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_ERROR( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_ERROR(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_DEBUG( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_DEBUG(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_FATAL( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_FATAL(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_TRACE(  char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_TRACE(m_logger, szMsg);
	END_LOG
}

void CLog::LOG_PACKET_INFO( char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_INFO(m_packetLogger, szMsg);
	END_LOG
}

void CLog::LOG_EFFICIENCY_INFO( const char* pFormat, ...)
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_INFO( m_efficiency, szMsg);
	END_LOG
}

void CLog::LOG_CLOSE_SOCKET_INFO( char * pFormat, ... )
{
	BEGIN_LOG
	char    szMsg[256];
	va_list pArg;
	va_start(pArg, pFormat);
	vsprintf_s(szMsg, 256, pFormat, pArg);
	va_end(pArg);
	LOG4CPLUS_INFO( m_closeSocket, szMsg);
	END_LOG
}
